CartController = {
	
    init: function (data) {
        this.model = data; 
        this.view = CartView;
        var cartList = CartController.getCartFromSessionStorage();
        this.view.displayCartList(new CartListModel(cartList));
    },

    convertToXML : function(cartList) {
        var result = "";
        result += "<carts>";
        for (var i = 0; i < cartList.length; i++) {
            result += "<cart>";
            result += "<id>";
            result += cartList[i].Id;
            result += "</id>";
            result += "<name>";
            result += cartList[i].Name;
            result += "</name>";
            result += "<author>";
            result += cartList[i].Author;
            result += "</author>";
            result += "<des>";
            result += cartList[i].Des;
            result += "</des>";
            result += "<price>";
            result += cartList[i].Price;
            result += "</price>";
            result += "<quantity>";
            result += cartList[i].Quantity;
            result += "</quantity>";
            result += "</cart>";
        }
        result += "</carts>";
        return result;
    },

    convertToJSON : function(cartList) {

    },

    onClick : function(id) {
        if(id == "cartViewButtonUpdate") {
            this.updateTotalAll();
        } else if (id == "cartViewButtonCheckout") {
            this.checkout();
        }
    },

    updateTotal: function(position, quantity) {
        try {
            var cartList = CartController.getCartFromSessionStorage();
            cartList[position].Quantity = quantity;
            CartController.setCartToSessionStorage(cartList);
            this.view.displayCartList(new CartListModel(cartList));
        } catch(ex) {
            alert("Error: " + ex.message);
        }
    },

    updateTotalAll: function() {
        try {
            var cartList = CartController.getCartFromSessionStorage();
            for (var i = 0; i < cartList.length; i++) {
                cartList[i].Quantity = $id(this.getNodeItemId() + "QuantityInput_" + i).value;
            }
            CartController.setCartToSessionStorage(cartList);
            this.view.displayCartList(new CartListModel(cartList));
        } catch (ex) {
            alert("Error: " + ex.message);
        }
    },

    checkout: function() {
        var self = this;
        var url = "";
        var cartList = this.getCartFromSessionStorage();

        var param = {
            cart: this.convertToXML(cartList),
            info: ""
        };

        var callback = {
            success: function(response) {
                var cartList = self.convertToJSON(response);
                self.view.displayCartList(new CartListModel(cartList));
                self.clearCartInSession();
            },
            failure: function(response) {
                alert("Error: " + JSON.stringify(response));
            },
            abort: function() {
                alert("Connection has problem");
            }
        };

        ajax.get({
            url: url,
            param: param,
            success: callback.success,
            failure: callback.failure,
            abort: callback.abort
        });
    }
};

CartController.SESSION_STORAGE_KEY = 'cartList';

CartController.setCartToSessionStorage = function(cartList) {
    sessionStorage.setItem(CartController.SESSION_STORAGE_KEY, JSON.stringify(cartList));
}

CartController.getCartFromSessionStorage = function() {
    var cartList = null;
    if (sessionStorage.getItem(CartController.SESSION_STORAGE_KEY)) {
        cartList = sessionStorage.getItem(CartController.SESSION_STORAGE_KEY);
        cartList = JSON.parse(cartList);
    } else {
        cartList = new Array();
    }
    return cartList;
}

CartController.clearCartInSession = function() {
    if (sessionStorage.getItem(CartController.SESSION_STORAGE_KEY)) {
        sessionStorage.removeItem(CartController.SESSION_STORAGE_KEY);
    }
}

CartController.checkItemInCart = function(model) {
    var cartDetail = new CartModel(model);
    var cartList = CartController.getCartFromSessionStorage();
    for(var i = 0; i < cartList.length; i++) {
        if(cartList[i].Id == cartDetail.Id) {
            return true;
        }
    }
    return false;
}

CartController.addToCart = function (model) {
    try {
        var cartDetail = new CartModel(model);
        var cartList = CartController.getCartFromSessionStorage();
        var isExist = CartController.checkItemInCart(cartDetail);
        cartList.push(cartDetail);
        this.setCartToSessionStorage(cartList);
        alert("Add to cart successful");
    } catch (ex) {
        alert("Failed when add to cart: " + ex.message);
    }
}